<?php

/**
 * 收藏
 * Class collect
 */
class collect {

    const Collected = 1;//喜欢
    const UnCollected = 2;//没喜欢

    private $_auth;
    private $db;
    private $db_log;
    private $db_content;

    public function  __construct() {
        $this->_auth = param::get_cookie('auth');
        $this->db = pc_base::load_model('collect_model');
        $this->db_log = pc_base::load_model('collect_log_model');
        $this->db_content = pc_base::load_model('content_model');

        if (!$this->_auth) {
            json_exit('请先登录', 201);
        }
    }

    //是否点赞
    public function is() {
        $uid = (int)param::get_cookie('_userid');

        $catid = $_POST['catid'] ? (int)$_POST['catid'] : 0;
        $id = $_POST['id'] ? (int)$_POST['id'] : 0;

        $sql = "uid='$uid' and a_catid='$catid' and a_id='$id'";
        $res = $this->db->get_one($sql);

        if ($res) {
            json_exit(self::Collected);
        } else {
            json_exit(self::UnCollected);
        }
    }

    //赞
    public function ex() {
        $uid = (int)param::get_cookie('_userid');

        $catid = $_POST['catid'] ? (int)$_POST['catid'] : 0;
        $id = $_POST['id'] ? (int)$_POST['id'] : 0;

        $sql = "uid='$uid' and a_catid='$catid' and a_id='$id'";
        $res = $this->db->get_one($sql);

        if ($res) {
            //取消赞
            $this->db->delete([
                'uid' => $uid,
                'a_catid' => $catid,
                'a_id' => $id,
            ]);

            //log
            $this->db_log->insert([
                'uid' => $uid,
                'a_catid' => $catid,
                'a_id' => $id,
                'ymd' => date('Y-m-d'),
                'ymdhis' => date('Y-m-d H:i:s'),
                'action' => 'cancel',
            ]);

            json_exit(self::UnCollected);
        } else {
            //赞
            $this->db->insert([
                'uid' => $uid,
                'a_catid' => $catid,
                'a_id' => $id,
                'ymd' => date('Y-m-d'),
                'ymdhis' => date('Y-m-d H:i:s'),
            ]);

            //log
            $this->db_log->insert([
                'uid' => $uid,
                'a_catid' => $catid,
                'a_id' => $id,
                'ymd' => date('Y-m-d'),
                'ymdhis' => date('Y-m-d H:i:s'),
                'action' => 'like',
            ]);

            if (in_array($catid, [1, 2, 3, 4, 5, 6])) {
                //更新数量
                $MODEL = getcache('model', 'commons');
                $CATEGORYS = getcache('category_content_1', 'commons');
                $modelid = $CATEGORYS[$catid]['modelid'];
                $table = $MODEL[$modelid]['tablename'];
                $this->db_content->table_name = 'v9_' . $table;
                $sql = "update {$this->db_content->table_name} set num_collect=num_collect+1 where id=$id";
                $this->db_content->query($sql);
            }

            json_exit(self::Collected);
        }
    }

    //赞
    public function guanzhu() {
        $uid = (int)param::get_cookie('_userid');

        $catid = $_POST['catid'] ? (int)$_POST['catid'] : 0;
        $id = $_POST['id'] ? (int)$_POST['id'] : 0;

        $sql = "uid='$uid' and a_catid='$catid' and a_id='$id'";
        $res = $this->db->get_one($sql);

        if ($res) {
            //取消赞
            $this->db->delete([
                'uid' => $uid,
                'a_catid' => $catid,
                'a_id' => $id,
            ]);
            echo 0;
            exit();
        } else {
            //赞
            $this->db->insert([
                'uid' => $uid,
                'a_catid' => $catid,
                'a_id' => $id,
                'ymd' => date('Y-m-d'),
                'ymdhis' => date('Y-m-d H:i:s'),
            ]);
            echo 1;
            exit();
        }
    }

}